﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharp_homework__2
{
    //-------------------------------------------------------------------------------------题目4，5-----------------------------------------------------------------------------------
    //各种形状的抽象类，兼职抽象产品类
    public abstract class Shape
    {
        private string myShapeType;                          //形状

        public Shape(string ShapeType)                         //构造函数
        {
            ShapeType = myShapeType;                             //把myShapeType的值给ShapeType
        }

        public string SType                                
        {
            get { return myShapeType; }
            set { myShapeType = value; }                   //value上下文关键字
        }

        public abstract double ShapeArea                 //抽象属性，面积。属性名后面没有小括号，方法有；抽象方法后面不能有方法体
        {
            get;
        }

        public virtual bool IsShapeLegal()                        //判断形状是否合法
        {
            return true;
        }

        public virtual void DrawShape()                          //绘制图形
        {
            Console.WriteLine("绘制图形") ;
        }
    }

    //正方形类
    public class Square : Shape
    {
        private int mySide;

        public Square (int side,string ShapeType) : base(ShapeType)                 
        {
            mySide = side;                                                  
        }

        public override double ShapeArea               //得到面积
        {
            get { return mySide * mySide; }
        }

        public override void DrawShape()               //绘制输出
        {
            if (base.IsShapeLegal() == true)
            {
                Console.WriteLine("这是个边长为{0}的正方形，面积为{1}",mySide,ShapeArea);
            }
            else { Console.WriteLine("该图形不合法，无法绘制！"); }
        }

    }

    //长方形类
    public class Rectangle : Shape
    {
        private int myWidth, myHeight;

        public Rectangle(int width, int height, string ShapeType) : base(ShapeType)                 //构造函数除了父类继承的SType，还有side
        {
            myWidth = width;
            myHeight = height;
        }

        public override double ShapeArea                                                    //面积
        {
            get { return myWidth * myHeight; }
        }

        public override bool IsShapeLegal()                        //判断形状是否合法
        {
            if (myHeight == myWidth) { return false; }
            return true;
        }

        public override void DrawShape()
        {
            if (IsShapeLegal() == true)
            {
                Console.WriteLine("这是个长宽为{0}，{1}的长方形；面积为{2}", myHeight,myWidth,ShapeArea);
            }
            else { Console.WriteLine("该图形不合法，无法绘制！"); }
        }

    }

    //三角形类
    public class Triangle : Shape
    {
        private double mySide1, mySide2, mySide3;

        public Triangle(double side1, double side2, double side3, string ShapeType) : base(ShapeType)                 //构造函数除了父类继承的SType，还有side
        {
            mySide1 = side1;
            mySide2 = side2;
            mySide3 = side3;
        }

        public override double ShapeArea                                                    //面积
        {
          
            get        
            {
                double myP = (mySide1 + mySide2 + mySide3) / 2;
                return Math.Sqrt( myP * (myP - mySide1) * (myP - mySide2) * (myP - mySide3));
            }
        }

        public override bool IsShapeLegal()                        //判断形状是否合法
        {
            if (mySide1 + mySide2 > mySide3 && mySide1 + mySide3 > mySide2 && mySide2 + mySide3 > mySide1) { return true; }
            return false;
        }

        public override void DrawShape()
        {
            if (IsShapeLegal() == true)
            {
                Console.WriteLine("这是个边长分别为为{0}，{1}，{2}的三角形；面积为{3}", mySide1, mySide2,mySide3,ShapeArea);
            }
            else { Console.WriteLine("该图形不合法，无法绘制！"); }
        }
    }

    //简单工厂类
    class Factory
    {
        public static Shape GetProduct(string shape)
        {
            Shape product = null;
            int[] side = new int[7];
            Random rd = new Random();

            switch (shape){                                                     //具体产品的选择生成
                case "正方形":

                        side[0] = rd.Next(1, 100);
                    product = new Square(side[0],shape);
                    break;
                case "长方形":
                    for (int i = 1; i < 3; i++)
                    {
                        side[i] = rd.Next(1, 100);
                    }
                    product = new Rectangle(side[1], side[2], shape);
                    break;
                case "三角形":
                    for (int i = 3; i <=5; i++)
                    {
                        side[i] = rd.Next(1, 100);
                    }
                    product = new Triangle(side[3], side[4], side[5], shape);
                    break;
                default:
                    Console.WriteLine("目前没有这类图形！");
                    break;
            }
            return product;
        }

    }

    class Program
    {
        //----------------------------------------------------------------------------------------------题目1-----------------------------------------------------------------
        //判断因子n是否为素数
        static bool JudgePeime(int n)                     
        {
            bool flag = true;
            if (n < 2) { flag = false; }
            else
            {
                for (int i = 2; i < n; i++)
                {
                    if (n % i == 0) { flag = false; }
                }
            }

            return flag;

        }

        //输出用户指定数据的所有素数因子
        static void Outprimenum(int primenum)
        {
            Console.WriteLine("所有素数因子为：");
            for(int i = 2;i <= primenum; i++)
            {
                while ((primenum % i) == 0)                      //若i是因子
                {
                    if (JudgePeime(i))                           //判断它是不是素数,如果是，就输出
                    {
                        Console.WriteLine("{0}   ",i);
                    }
                    primenum /= i;
                }
            }
            
        }

        //--------------------------------------------------------------------------------题目2------------------------------------------------------------------------------------------
        //求一个整数数组的最大值、最小值、平均值和所有数组元素的和
        static void GetArrayValue(int[] intarray)
        {
            int L = intarray.Length;                                   //通过L得到数组的长度
            int sum = 0;                                               //sum为数组的和
            int maxint = intarray[0];                                  //最大值
            int minint=intarray[0];                                    //最小值
            for(int i = 0; i < L; i++)
            {
                if (maxint<=intarray[i])                                //当前数组元素的值和之前数组元素的最大值进行比较，比它大，就取代
                {
                    maxint = intarray[i];
                }
                if (intarray[i] <= minint)
                {
                    minint = intarray[i];
                }
                sum += intarray[i];
            }
            Console.WriteLine("这个数组的所有元素的和为：{0}",sum);
            Console.WriteLine("这个数组的所有元素的平均值为{0}",sum/L);
            Console.WriteLine("这个数组的所有元素的最小为{0}", minint);
            Console.WriteLine("这个数组的所有元素的最大值为{0}", maxint);
        }

        //-----------------------------------------------------------------------------题目3------------------------------------------------------------------------------------------
        //用埃氏法求2-100以内的素数
        static void ALaw()
        {
            bool[] IsPrime = new bool[101];                                 //搞个bool数组
            for(int i = 0; i <= 100; i++)
            {
                IsPrime[i] = true;
            }
            for(int i = 2; i <= Math.Sqrt(100); i++)                        //2-10分别对2-100以内的数进行除
            {
                for(int j = 2; j <= 100; j++)
                {
                    if (j % i == 0&&i!=j) { IsPrime[j] = false; }                 //
                }
            }
            for(int i = 2; i <= 100; i++)
            {
                if (IsPrime[i]==true){Console.WriteLine("{0}", i); }
            }
        }

        static void Main(string[] args)
        {
            //输出素数因子----------------------------------------------------------------------------------执行1-------------------------------------------------------------------------
            int primenum;                                             //输入的正整数
            Console.WriteLine("请输入一个正整数：");
            primenum = int.Parse(Console.ReadLine());                //输入
            Outprimenum(primenum);                                    //引用非静态的，前面得加上名字.，
            Console.WriteLine("!");

            //编程求一个整数数组的最大值、最小值、平均值和所有数组元素的和-------------------------------------执行2--------------------------------------------------------------------------
            int[] intarray = { 3, 4, 5, 7, 2, 8, 9, 0, 1, 6 };                        //数组
            Console.WriteLine("现有一个数组，它的值为：3，4，5，7，2，8，9，1，6。");
            GetArrayValue(intarray);                                                //求值

            //用埃氏法求2-100以内的素数------------------------------------------------------------------------执行3---------------------------------------------------------------------------
            Console.WriteLine("用埃氏法求2-100以内的素数，其结果为：");
            ALaw();

            //编写面向对象程序实现长方形、正方形、三角形类，每个形状都能计算面积，判断形状是否合法------------------------------------执行4------------------------------------------------------
            Shape[] Shapes1 =
            {
                new Square(5,"正方形"),
                new Rectangle(5,5,"长方形"),
                new Rectangle(4,5,"长方形"),
                new Triangle(1,2,3,"三角形"),
                new Triangle(3,3,3,"三角形")
            };
            foreach(Shape s in Shapes1)                        //输出
            {
                s.DrawShape();
            }

            //简单工厂模式，随即创建10个形状对象，计算这些对象面积之和---------------------------------------------------------------------执行5-------------------------------------------
            Shape[] myProduct= new Shape[10];               //放十个对象的数
            Random rd = new Random();       
            string shape;
            double sum=0;                                   //用于存放面积之和

            for (int i = 0; i < 10; i++)
            {
                switch (rd.Next(1, 4))                 //借用随机数生成随机字符
                {
                    case 1:
                        shape = "正方形";
                        break;
                    case 2:
                        shape = "长方形";
                        break;
                    case 3:
                        shape = "三角形";
                        break;
                    default:
                        shape = "无形状";
                        break;
                }

                
                myProduct[i] = ((Factory.GetProduct(shape)));
                myProduct[i].DrawShape();
                if (myProduct[i].IsShapeLegal() == true)
                {
                    
                    sum += myProduct[i].ShapeArea;
                }
                System.Threading.Thread.Sleep(10);             //为了随机数起作用，暂停一下下
            }
            Console.WriteLine("面积之和为：{0}",sum);
            Console.WriteLine("!");
        }
    }
}
